////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// This file is part of the Microsoft .NET Micro Framework Porting Kit Code Samples and is unsupported.
// Copyright (C) Microsoft Corporation. All rights reserved. Use of this sample source code is subject to
// the terms of the Microsoft license agreement under which you licensed this sample source code.
//
// THIS SAMPLE CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <tinyhal.h>

//--//
BOOL I2C_Initialize()
{
    NATIVE_PROFILE_PAL_COM();
    return FALSE;
}

BOOL I2C_Uninitialize()
{
    NATIVE_PROFILE_PAL_COM();
    return FALSE;
}

BOOL I2C_Enqueue( I2C_HAL_XACTION* xAction )
{
    NATIVE_PROFILE_PAL_COM();
    return FALSE;
}

BOOL I2C_EnqueueEx( I2C_HAL_XACTION* xAction, HAL_CALLBACK_FPN xActionCompleted )
{
    NATIVE_PROFILE_PAL_COM();
    return FALSE;
}

void I2C_Cancel( I2C_HAL_XACTION* xAction, bool signal )
{
    NATIVE_PROFILE_PAL_COM();
}

void I2C_InitializeTransaction( I2C_HAL_XACTION* xAction, I2C_USER_CONFIGURATION& config, I2C_HAL_XACTION_UNIT** xActions, size_t numXActions )
{
    NATIVE_PROFILE_PAL_COM();
}

void I2C_InitializeTransactionUnit( I2C_HAL_XACTION_UNIT* xActionUnit, I2C_WORD* src, I2C_WORD* dst, size_t size, BOOL fRead )
{
    NATIVE_PROFILE_PAL_COM();
}

//--//

void I2C_XAction_SetState( I2C_HAL_XACTION* xAction, UINT8 state )
{
    NATIVE_PROFILE_PAL_COM();
}

UINT8 I2C_XAction_GetState( I2C_HAL_XACTION* xAction )
{
    NATIVE_PROFILE_PAL_COM();
    return 0;
}

BOOL I2C_XAction_CheckState( I2C_HAL_XACTION* xAction, UINT8 state )
{
    NATIVE_PROFILE_PAL_COM();
    return FALSE;
}

size_t I2C_XAction_TransactedBytes( I2C_HAL_XACTION* xAction )
{
    NATIVE_PROFILE_PAL_COM();
    return 0;
}

void I2C_XActionUnit_CopyBuffer( I2C_HAL_XACTION_UNIT* xActionUnit, I2C_WORD* data, size_t length )
{
    NATIVE_PROFILE_PAL_COM();
}

BOOL I2C_XActionUnit_IsRead( I2C_HAL_XACTION_UNIT* xActionUnit )
{
    NATIVE_PROFILE_PAL_COM();
    return FALSE;
}

////////////////////////////////////////////////////////////////////////////////

void I2C_HAL_XACTION_UNIT::Initialize( I2C_WORD* src, I2C_WORD* dst, size_t size, BOOL fRead )
{
    NATIVE_PROFILE_PAL_COM();
    m_bytesToTransfer  = size;
    m_bytesTransferred = 0;
    m_fRead            = fRead;

    m_dataQueue.Initialize( dst, size + 1 );

    if(!fRead)
    {
        I2C_WORD* slot;

        while((slot = m_dataQueue.Push()) != NULL)
        {
            *slot = *src++;
        }
    }
}

///////////////////////////////////////////////////////////////////////////////

void I2C_HAL_XACTION::Initialize( I2C_USER_CONFIGURATION& config, I2C_HAL_XACTION_UNIT** xActionUnits, size_t numXActionUnits )
{
    NATIVE_PROFILE_PAL_COM();
    m_xActionUnits    = xActionUnits;
    m_numXActionUnits = numXActionUnits;
    m_current         = 0;
    I2C_Internal_GetClockRate( config.ClockRate, m_clockRate, m_clockRate2);	
    m_address         = config.Address;
    m_status          = c_Status_Idle;
}

void I2C_HAL_XACTION::Signal( UINT8 state, BOOL signal )
{
    NATIVE_PROFILE_PAL_COM();
    ASSERT_IRQ_MUST_BE_OFF();

    m_status = state;

    // this will automatically remove this node from the I2C_Driver
    // list and place it in the global continuation list
    if(signal) Enqueue();

    // stop the flow if the transaction was not cancelled
    // in whihc case the Stop command would have been issued already
    if(!(m_status & c_Status_Cancelled))
    {
        I2C_Internal_XActionStop();
    }
}

///////////////////////////////////////////////////////////////////////////////


